package net.sudot.chess.task;

import net.sudot.chess.business.model.Room;
import net.sudot.chess.business.service.RoomService;
import net.sudot.chess.util.WBihuizhiUtils;
import net.sudot.commons.lock.Lock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 房间任务
 *
 * @author tangjialin on 2018-11-12.
 */
@Component
public class RoomTask {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Resource
    private RoomService roomService;

    /**
     * 房间过期处理
     */
    @Lock(name = "lock:scheduled:room:expired:processing")
//    @Scheduled(cron = "${scheduled.room.expired.processing.cron}")
    public void expiredProcessing() {
        List<Room> roomList = roomService.findAllListAtPlaying();
        for (Room room : roomList) {
            if (room.getUrl() == null) { continue; }
            WBihuizhiUtils.RoomStatus status = WBihuizhiUtils.getRoomStatus(WBihuizhiUtils.getHtml(room.getUrl()));
            if (status != WBihuizhiUtils.RoomStatus.CANCEL) { continue;}
            try {
                roomService.cancel(room);
            } catch (Exception e) {
                logger.error("房间过期处理异常", e);
            }
        }
    }
}
